<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>ierange.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_browserrange_ierange.js.html">ierange.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the IE browser specific range wrapper.
<a name="line17"></a> *
<a name="line18"></a> * DO NOT USE THIS FILE DIRECTLY.  Use goog.dom.Range instead.
<a name="line19"></a> *
<a name="line20"></a> * @author robbyw@google.com (Robby Walker)
<a name="line21"></a> * @author ojan@google.com (Ojan Vafai)
<a name="line22"></a> * @author jparent@google.com (Julie Parent)
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.dom.browserrange.IeRange&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.array&#39;);
<a name="line29"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line30"></a>goog.require(&#39;goog.dom&#39;);
<a name="line31"></a>goog.require(&#39;goog.dom.NodeIterator&#39;);
<a name="line32"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line33"></a>goog.require(&#39;goog.dom.RangeEndpoint&#39;);
<a name="line34"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line35"></a>goog.require(&#39;goog.dom.browserrange.AbstractRange&#39;);
<a name="line36"></a>goog.require(&#39;goog.iter&#39;);
<a name="line37"></a>goog.require(&#39;goog.iter.StopIteration&#39;);
<a name="line38"></a>goog.require(&#39;goog.string&#39;);
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * The constructor for IE specific browser ranges.
<a name="line44"></a> * @param {TextRange} range The range object.
<a name="line45"></a> * @param {Document} doc The document the range exists in.
<a name="line46"></a> * @constructor
<a name="line47"></a> * @extends {goog.dom.browserrange.AbstractRange}
<a name="line48"></a> */
<a name="line49"></a>goog.dom.browserrange.IeRange = function(range, doc) {
<a name="line50"></a>  /**
<a name="line51"></a>   * The browser range object this class wraps.
<a name="line52"></a>   * @type {TextRange}
<a name="line53"></a>   * @private
<a name="line54"></a>   */
<a name="line55"></a>  this.range_ = range;
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * The document the range exists in.
<a name="line59"></a>   * @type {Document}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.doc_ = doc;
<a name="line63"></a>};
<a name="line64"></a>goog.inherits(goog.dom.browserrange.IeRange,
<a name="line65"></a>    goog.dom.browserrange.AbstractRange);
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Logging object.
<a name="line70"></a> * @type {goog.debug.Logger}
<a name="line71"></a> * @private
<a name="line72"></a> */
<a name="line73"></a>goog.dom.browserrange.IeRange.logger_ =
<a name="line74"></a>    goog.debug.Logger.getLogger(&#39;goog.dom.browserrange.IeRange&#39;);
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Returns a browser range spanning the given node&#39;s contents.
<a name="line79"></a> * @param {Node} node The node to select.
<a name="line80"></a> * @return {TextRange} A browser range spanning the node&#39;s contents.
<a name="line81"></a> * @private
<a name="line82"></a> */
<a name="line83"></a>goog.dom.browserrange.IeRange.getBrowserRangeForNode_ = function(node) {
<a name="line84"></a>  var nodeRange = goog.dom.getOwnerDocument(node).body.createTextRange();
<a name="line85"></a>  if (node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line86"></a>    // Elements are easy.
<a name="line87"></a>    nodeRange.moveToElementText(node);
<a name="line88"></a>    // Note(user) : If there are no child nodes of the element, the
<a name="line89"></a>    // range.htmlText includes the element&#39;s outerHTML. The range created above
<a name="line90"></a>    // is not collapsed, and should be collapsed explicitly.
<a name="line91"></a>    // Example : node = &lt;div&gt;&lt;/div&gt;
<a name="line92"></a>    // But if the node is sth like &lt;br&gt;, it shouldnt be collapsed.
<a name="line93"></a>    if (goog.dom.browserrange.canContainRangeEndpoint(node) &amp;&amp;
<a name="line94"></a>        !node.childNodes.length) {
<a name="line95"></a>      nodeRange.collapse(false);
<a name="line96"></a>    }
<a name="line97"></a>  } else {
<a name="line98"></a>    // Text nodes are hard.
<a name="line99"></a>    // Compute the offset from the nearest element related position.
<a name="line100"></a>    var offset = 0;
<a name="line101"></a>    var sibling = node;
<a name="line102"></a>    while (sibling = sibling.previousSibling) {
<a name="line103"></a>      var nodeType = sibling.nodeType;
<a name="line104"></a>      if (nodeType == goog.dom.NodeType.TEXT) {
<a name="line105"></a>        offset += sibling.length;
<a name="line106"></a>      } else if (nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line107"></a>        // Move to the space after this element.
<a name="line108"></a>        nodeRange.moveToElementText(sibling);
<a name="line109"></a>        break;
<a name="line110"></a>      }
<a name="line111"></a>    }
<a name="line112"></a>
<a name="line113"></a>    if (!sibling) {
<a name="line114"></a>      nodeRange.moveToElementText(node.parentNode);
<a name="line115"></a>    }
<a name="line116"></a>
<a name="line117"></a>    nodeRange.collapse(!sibling);
<a name="line118"></a>
<a name="line119"></a>    if (offset) {
<a name="line120"></a>      nodeRange.move(&#39;character&#39;, offset);
<a name="line121"></a>    }
<a name="line122"></a>
<a name="line123"></a>    nodeRange.moveEnd(&#39;character&#39;, node.length);
<a name="line124"></a>  }
<a name="line125"></a>
<a name="line126"></a>  return nodeRange;
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Returns a browser range spanning the given nodes.
<a name="line132"></a> * @param {Node} startNode The node to start with.
<a name="line133"></a> * @param {number} startOffset The offset within the start node.
<a name="line134"></a> * @param {Node} endNode The node to end with.
<a name="line135"></a> * @param {number} endOffset The offset within the end node.
<a name="line136"></a> * @return {TextRange} A browser range spanning the node&#39;s contents.
<a name="line137"></a> * @private
<a name="line138"></a> */
<a name="line139"></a>goog.dom.browserrange.IeRange.getBrowserRangeForNodes_ = function(startNode,
<a name="line140"></a>    startOffset, endNode, endOffset) {
<a name="line141"></a>  // Create a range starting at the correct start position.
<a name="line142"></a>  var child, collapse = false;
<a name="line143"></a>  if (startNode.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line144"></a>    if (startOffset &gt; startNode.childNodes.length) {
<a name="line145"></a>      goog.dom.browserrange.IeRange.logger_.severe(
<a name="line146"></a>          &#39;Cannot have startOffset &gt; startNode child count&#39;);
<a name="line147"></a>    }
<a name="line148"></a>    child = startNode.childNodes[startOffset];
<a name="line149"></a>    collapse = !child;
<a name="line150"></a>    startNode = child || startNode.lastChild || startNode;
<a name="line151"></a>    startOffset = 0;
<a name="line152"></a>  }
<a name="line153"></a>  var leftRange = goog.dom.browserrange.IeRange.
<a name="line154"></a>      getBrowserRangeForNode_(startNode);
<a name="line155"></a>
<a name="line156"></a>  // This happens only when startNode is a text node.
<a name="line157"></a>  if (startOffset) {
<a name="line158"></a>    leftRange.move(&#39;character&#39;, startOffset);
<a name="line159"></a>  }
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>  // The range movements in IE are still an approximation to the standard W3C
<a name="line163"></a>  // behavior, and IE has its trickery when it comes to htmlText and text
<a name="line164"></a>  // properties of the range. So we short-circuit computation whenever we can.
<a name="line165"></a>  if (startNode == endNode &amp;&amp; startOffset == endOffset) {
<a name="line166"></a>    leftRange.collapse(true);
<a name="line167"></a>    return leftRange;
<a name="line168"></a>  }
<a name="line169"></a>
<a name="line170"></a>  // This can happen only when the startNode is an element, and there is no node
<a name="line171"></a>  // at the given offset. We start at the last point inside the startNode in
<a name="line172"></a>  // that case.
<a name="line173"></a>  if (collapse) {
<a name="line174"></a>    leftRange.collapse(false);
<a name="line175"></a>  }
<a name="line176"></a>
<a name="line177"></a>  // Create a range that ends at the right position.
<a name="line178"></a>  collapse = false;
<a name="line179"></a>  if (endNode.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line180"></a>    if (endOffset &gt; endNode.childNodes.length) {
<a name="line181"></a>      goog.dom.browserrange.IeRange.logger_.severe(
<a name="line182"></a>          &#39;Cannot have endOffset &gt; endNode child count&#39;);
<a name="line183"></a>    }
<a name="line184"></a>    child = endNode.childNodes[endOffset];
<a name="line185"></a>    endNode = child || endNode.lastChild || endNode;
<a name="line186"></a>    endOffset = 0;
<a name="line187"></a>    collapse = !child;
<a name="line188"></a>  }
<a name="line189"></a>  var rightRange = goog.dom.browserrange.IeRange.
<a name="line190"></a>      getBrowserRangeForNode_(endNode);
<a name="line191"></a>  rightRange.collapse(!collapse);
<a name="line192"></a>  if (endOffset) {
<a name="line193"></a>    rightRange.moveEnd(&#39;character&#39;, endOffset);
<a name="line194"></a>  }
<a name="line195"></a>
<a name="line196"></a>  // Merge and return.
<a name="line197"></a>  leftRange.setEndPoint(&#39;EndToEnd&#39;, rightRange);
<a name="line198"></a>  return leftRange;
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>/**
<a name="line203"></a> * Create a range object that selects the given node&#39;s text.
<a name="line204"></a> * @param {Node} node The node to select.
<a name="line205"></a> * @return {goog.dom.browserrange.IeRange} An IE range wrapper object.
<a name="line206"></a> */
<a name="line207"></a>goog.dom.browserrange.IeRange.createFromNodeContents = function(node) {
<a name="line208"></a>  var range = new goog.dom.browserrange.IeRange(
<a name="line209"></a>      goog.dom.browserrange.IeRange.getBrowserRangeForNode_(node),
<a name="line210"></a>      goog.dom.getOwnerDocument(node));
<a name="line211"></a>
<a name="line212"></a>  if (!goog.dom.browserrange.canContainRangeEndpoint(node)) {
<a name="line213"></a>    range.startNode_ = range.endNode_ = range.parentNode_ = node.parentNode;
<a name="line214"></a>    range.startOffset_ = goog.array.indexOf(range.parentNode_.childNodes, node);
<a name="line215"></a>    range.endOffset_ = range.startOffset_ + 1;
<a name="line216"></a>  } else {
<a name="line217"></a>    // Note(user) : Emulate the behavior of W3CRange - Go to deepest possible
<a name="line218"></a>    // range containers on both edges. It seems W3CRange did this to match the
<a name="line219"></a>    // IE behavior, and now it is a circle. Changing W3CRange may break clients
<a name="line220"></a>    // in all sorts of ways.
<a name="line221"></a>    var tempNode, leaf = node;
<a name="line222"></a>    while ((tempNode = leaf.firstChild) &amp;&amp;
<a name="line223"></a>           goog.dom.browserrange.canContainRangeEndpoint(tempNode)) {
<a name="line224"></a>      leaf = tempNode;
<a name="line225"></a>    }
<a name="line226"></a>    range.startNode_ = leaf;
<a name="line227"></a>    range.startOffset_ = 0;
<a name="line228"></a>
<a name="line229"></a>    leaf = node;
<a name="line230"></a>    while ((tempNode = leaf.lastChild) &amp;&amp;
<a name="line231"></a>           goog.dom.browserrange.canContainRangeEndpoint(tempNode)) {
<a name="line232"></a>      leaf = tempNode;
<a name="line233"></a>    }
<a name="line234"></a>    range.endNode_ = leaf;
<a name="line235"></a>    range.endOffset_ = leaf.nodeType == goog.dom.NodeType.ELEMENT ?
<a name="line236"></a>                       leaf.childNodes.length : leaf.length;
<a name="line237"></a>    range.parentNode_ = node;
<a name="line238"></a>  }
<a name="line239"></a>  return range;
<a name="line240"></a>};
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/**
<a name="line244"></a> * Static method that returns the proper type of browser range.
<a name="line245"></a> * @param {Node} startNode The node to start with.
<a name="line246"></a> * @param {number} startOffset The offset within the start node.
<a name="line247"></a> * @param {Node} endNode The node to end with.
<a name="line248"></a> * @param {number} endOffset The offset within the end node.
<a name="line249"></a> * @return {goog.dom.browserrange.AbstractRange} A wrapper object.
<a name="line250"></a> */
<a name="line251"></a>goog.dom.browserrange.IeRange.createFromNodes = function(startNode,
<a name="line252"></a>    startOffset, endNode, endOffset) {
<a name="line253"></a>  var range = new goog.dom.browserrange.IeRange(
<a name="line254"></a>      goog.dom.browserrange.IeRange.getBrowserRangeForNodes_(startNode,
<a name="line255"></a>          startOffset, endNode, endOffset),
<a name="line256"></a>      goog.dom.getOwnerDocument(startNode));
<a name="line257"></a>  range.startNode_ = startNode;
<a name="line258"></a>  range.startOffset_ = startOffset;
<a name="line259"></a>  range.endNode_ = endNode;
<a name="line260"></a>  range.endOffset_ = endOffset;
<a name="line261"></a>  return range;
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>// Even though goog.dom.TextRange does similar caching to below, keeping these
<a name="line266"></a>// caches allows for better performance in the get*Offset methods.
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>/**
<a name="line270"></a> * Lazy cache of the node containing the entire selection.
<a name="line271"></a> * @type {Node}
<a name="line272"></a> * @private
<a name="line273"></a> */
<a name="line274"></a>goog.dom.browserrange.IeRange.prototype.parentNode_ = null;
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/**
<a name="line278"></a> * Lazy cache of the node containing the start of the selection.
<a name="line279"></a> * @type {Node}
<a name="line280"></a> * @private
<a name="line281"></a> */
<a name="line282"></a>goog.dom.browserrange.IeRange.prototype.startNode_ = null;
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * Lazy cache of the node containing the end of the selection.
<a name="line287"></a> * @type {Node}
<a name="line288"></a> * @private
<a name="line289"></a> */
<a name="line290"></a>goog.dom.browserrange.IeRange.prototype.endNode_ = null;
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>/**
<a name="line294"></a> * Lazy cache of the offset in startNode_ where this range starts.
<a name="line295"></a> * @type {number}
<a name="line296"></a> * @private
<a name="line297"></a> */
<a name="line298"></a>goog.dom.browserrange.IeRange.prototype.startOffset_ = -1;
<a name="line299"></a>
<a name="line300"></a>
<a name="line301"></a>/**
<a name="line302"></a> * Lazy cache of the offset in endNode_ where this range ends.
<a name="line303"></a> * @type {number}
<a name="line304"></a> * @private
<a name="line305"></a> */
<a name="line306"></a>goog.dom.browserrange.IeRange.prototype.endOffset_ = -1;
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * @return {goog.dom.browserrange.IeRange} A clone of this range.
<a name="line311"></a> * @override
<a name="line312"></a> */
<a name="line313"></a>goog.dom.browserrange.IeRange.prototype.clone = function() {
<a name="line314"></a>  var range = new goog.dom.browserrange.IeRange(
<a name="line315"></a>      this.range_.duplicate(), this.doc_);
<a name="line316"></a>  range.parentNode_ = this.parentNode_;
<a name="line317"></a>  range.startNode_ = this.startNode_;
<a name="line318"></a>  range.endNode_ = this.endNode_;
<a name="line319"></a>  return range;
<a name="line320"></a>};
<a name="line321"></a>
<a name="line322"></a>
<a name="line323"></a>/** @override */
<a name="line324"></a>goog.dom.browserrange.IeRange.prototype.getBrowserRange = function() {
<a name="line325"></a>  return this.range_;
<a name="line326"></a>};
<a name="line327"></a>
<a name="line328"></a>
<a name="line329"></a>/**
<a name="line330"></a> * Clears the cached values for containers.
<a name="line331"></a> * @private
<a name="line332"></a> */
<a name="line333"></a>goog.dom.browserrange.IeRange.prototype.clearCachedValues_ = function() {
<a name="line334"></a>  this.parentNode_ = this.startNode_ = this.endNode_ = null;
<a name="line335"></a>  this.startOffset_ = this.endOffset_ = -1;
<a name="line336"></a>};
<a name="line337"></a>
<a name="line338"></a>
<a name="line339"></a>/** @override */
<a name="line340"></a>goog.dom.browserrange.IeRange.prototype.getContainer = function() {
<a name="line341"></a>  if (!this.parentNode_) {
<a name="line342"></a>    var selectText = this.range_.text;
<a name="line343"></a>
<a name="line344"></a>    // If the selection ends with spaces, we need to remove these to get the
<a name="line345"></a>    // parent container of only the real contents.  This is to get around IE&#39;s
<a name="line346"></a>    // inconsistency where it selects the spaces after a word when you double
<a name="line347"></a>    // click, but leaves out the spaces during execCommands.
<a name="line348"></a>    var range = this.range_.duplicate();
<a name="line349"></a>    // We can&#39;t use goog.string.trimRight, as that will remove other whitespace
<a name="line350"></a>    // too.
<a name="line351"></a>    var rightTrimmedSelectText = selectText.replace(/ +$/, &#39;&#39;);
<a name="line352"></a>    var numSpacesAtEnd = selectText.length - rightTrimmedSelectText.length;
<a name="line353"></a>    if (numSpacesAtEnd) {
<a name="line354"></a>      range.moveEnd(&#39;character&#39;, -numSpacesAtEnd);
<a name="line355"></a>    }
<a name="line356"></a>
<a name="line357"></a>    // Get the parent node.  This should be the end, but alas, it is not.
<a name="line358"></a>    var parent = range.parentElement();
<a name="line359"></a>
<a name="line360"></a>    var htmlText = range.htmlText;
<a name="line361"></a>    var htmlTextLen = goog.string.stripNewlines(htmlText).length;
<a name="line362"></a>    if (this.isCollapsed() &amp;&amp; htmlTextLen &gt; 0) {
<a name="line363"></a>      return (this.parentNode_ = parent);
<a name="line364"></a>    }
<a name="line365"></a>
<a name="line366"></a>    // Deal with selection bug where IE thinks one of the selection&#39;s children
<a name="line367"></a>    // is actually the selection&#39;s parent. Relies on the assumption that the
<a name="line368"></a>    // HTML text of the parent container is longer than the length of the
<a name="line369"></a>    // selection&#39;s HTML text.
<a name="line370"></a>
<a name="line371"></a>    // Also note IE will sometimes insert \r and \n whitespace, which should be
<a name="line372"></a>    // disregarded. Otherwise the loop may run too long and return wrong parent
<a name="line373"></a>    while (htmlTextLen &gt; goog.string.stripNewlines(parent.outerHTML).length) {
<a name="line374"></a>      parent = parent.parentNode;
<a name="line375"></a>    }
<a name="line376"></a>
<a name="line377"></a>    // Deal with IE&#39;s selecting the outer tags when you double click
<a name="line378"></a>    // If the innerText is the same, then we just want the inner node
<a name="line379"></a>    while (parent.childNodes.length == 1 &amp;&amp;
<a name="line380"></a>           parent.innerText == goog.dom.browserrange.IeRange.getNodeText_(
<a name="line381"></a>               parent.firstChild)) {
<a name="line382"></a>      // A container should be an element which can have children or a text
<a name="line383"></a>      // node. Elements like IMG, BR, etc. can not be containers.
<a name="line384"></a>      if (!goog.dom.browserrange.canContainRangeEndpoint(parent.firstChild)) {
<a name="line385"></a>        break;
<a name="line386"></a>      }
<a name="line387"></a>      parent = parent.firstChild;
<a name="line388"></a>    }
<a name="line389"></a>
<a name="line390"></a>    // If the selection is empty, we may need to do extra work to position it
<a name="line391"></a>    // properly.
<a name="line392"></a>    if (selectText.length == 0) {
<a name="line393"></a>      parent = this.findDeepestContainer_(parent);
<a name="line394"></a>    }
<a name="line395"></a>
<a name="line396"></a>    this.parentNode_ = parent;
<a name="line397"></a>  }
<a name="line398"></a>
<a name="line399"></a>  return this.parentNode_;
<a name="line400"></a>};
<a name="line401"></a>
<a name="line402"></a>
<a name="line403"></a>/**
<a name="line404"></a> * Helper method to find the deepest parent for this range, starting
<a name="line405"></a> * the search from {@code node}, which must contain the range.
<a name="line406"></a> * @param {Node} node The node to start the search from.
<a name="line407"></a> * @return {Node} The deepest parent for this range.
<a name="line408"></a> * @private
<a name="line409"></a> */
<a name="line410"></a>goog.dom.browserrange.IeRange.prototype.findDeepestContainer_ = function(node) {
<a name="line411"></a>  var childNodes = node.childNodes;
<a name="line412"></a>  for (var i = 0, len = childNodes.length; i &lt; len; i++) {
<a name="line413"></a>    var child = childNodes[i];
<a name="line414"></a>
<a name="line415"></a>    if (goog.dom.browserrange.canContainRangeEndpoint(child)) {
<a name="line416"></a>      var childRange =
<a name="line417"></a>          goog.dom.browserrange.IeRange.getBrowserRangeForNode_(child);
<a name="line418"></a>      var start = goog.dom.RangeEndpoint.START;
<a name="line419"></a>      var end = goog.dom.RangeEndpoint.END;
<a name="line420"></a>
<a name="line421"></a>      // There are two types of erratic nodes where the range over node has
<a name="line422"></a>      // different htmlText than the node&#39;s outerHTML.
<a name="line423"></a>      // Case 1 - A node with magic &amp;nbsp; child. In this case :
<a name="line424"></a>      //    nodeRange.htmlText shows &amp;nbsp; (&#39;&lt;p&gt;&amp;nbsp;&lt;/p&gt;), while
<a name="line425"></a>      //    node.outerHTML doesn&#39;t show the magic node (&lt;p&gt;&lt;/p&gt;).
<a name="line426"></a>      // Case 2 - Empty span. In this case :
<a name="line427"></a>      //    node.outerHTML shows &#39;&lt;span&gt;&lt;/span&gt;&#39;
<a name="line428"></a>      //    node.htmlText is just empty string &#39;&#39;.
<a name="line429"></a>      var isChildRangeErratic = (childRange.htmlText != child.outerHTML);
<a name="line430"></a>
<a name="line431"></a>      // Moreover the inRange comparison fails only when the
<a name="line432"></a>      var isNativeInRangeErratic = this.isCollapsed() &amp;&amp; isChildRangeErratic;
<a name="line433"></a>
<a name="line434"></a>      // In case 2 mentioned above, childRange is also collapsed. So we need to
<a name="line435"></a>      // compare start of this range with both start and end of child range.
<a name="line436"></a>      var inChildRange = isNativeInRangeErratic ?
<a name="line437"></a>          (this.compareBrowserRangeEndpoints(childRange, start, start) &gt;= 0 &amp;&amp;
<a name="line438"></a>              this.compareBrowserRangeEndpoints(childRange, start, end) &lt;= 0) :
<a name="line439"></a>           this.range_.inRange(childRange);
<a name="line440"></a>      if (inChildRange) {
<a name="line441"></a>        return this.findDeepestContainer_(child);
<a name="line442"></a>      }
<a name="line443"></a>    }
<a name="line444"></a>  }
<a name="line445"></a>
<a name="line446"></a>  return node;
<a name="line447"></a>};
<a name="line448"></a>
<a name="line449"></a>
<a name="line450"></a>/** @override */
<a name="line451"></a>goog.dom.browserrange.IeRange.prototype.getStartNode = function() {
<a name="line452"></a>  if (!this.startNode_) {
<a name="line453"></a>    this.startNode_ = this.getEndpointNode_(goog.dom.RangeEndpoint.START);
<a name="line454"></a>    if (this.isCollapsed()) {
<a name="line455"></a>      this.endNode_ = this.startNode_;
<a name="line456"></a>    }
<a name="line457"></a>  }
<a name="line458"></a>  return this.startNode_;
<a name="line459"></a>};
<a name="line460"></a>
<a name="line461"></a>
<a name="line462"></a>/** @override */
<a name="line463"></a>goog.dom.browserrange.IeRange.prototype.getStartOffset = function() {
<a name="line464"></a>  if (this.startOffset_ &lt; 0) {
<a name="line465"></a>    this.startOffset_ = this.getOffset_(goog.dom.RangeEndpoint.START);
<a name="line466"></a>    if (this.isCollapsed()) {
<a name="line467"></a>      this.endOffset_ = this.startOffset_;
<a name="line468"></a>    }
<a name="line469"></a>  }
<a name="line470"></a>  return this.startOffset_;
<a name="line471"></a>};
<a name="line472"></a>
<a name="line473"></a>
<a name="line474"></a>/** @override */
<a name="line475"></a>goog.dom.browserrange.IeRange.prototype.getEndNode = function() {
<a name="line476"></a>  if (this.isCollapsed()) {
<a name="line477"></a>    return this.getStartNode();
<a name="line478"></a>  }
<a name="line479"></a>  if (!this.endNode_) {
<a name="line480"></a>    this.endNode_ = this.getEndpointNode_(goog.dom.RangeEndpoint.END);
<a name="line481"></a>  }
<a name="line482"></a>  return this.endNode_;
<a name="line483"></a>};
<a name="line484"></a>
<a name="line485"></a>
<a name="line486"></a>/** @override */
<a name="line487"></a>goog.dom.browserrange.IeRange.prototype.getEndOffset = function() {
<a name="line488"></a>  if (this.isCollapsed()) {
<a name="line489"></a>    return this.getStartOffset();
<a name="line490"></a>  }
<a name="line491"></a>  if (this.endOffset_ &lt; 0) {
<a name="line492"></a>    this.endOffset_ = this.getOffset_(goog.dom.RangeEndpoint.END);
<a name="line493"></a>    if (this.isCollapsed()) {
<a name="line494"></a>      this.startOffset_ = this.endOffset_;
<a name="line495"></a>    }
<a name="line496"></a>  }
<a name="line497"></a>  return this.endOffset_;
<a name="line498"></a>};
<a name="line499"></a>
<a name="line500"></a>
<a name="line501"></a>/** @override */
<a name="line502"></a>goog.dom.browserrange.IeRange.prototype.compareBrowserRangeEndpoints = function(
<a name="line503"></a>    range, thisEndpoint, otherEndpoint) {
<a name="line504"></a>  return this.range_.compareEndPoints(
<a name="line505"></a>      (thisEndpoint == goog.dom.RangeEndpoint.START ? &#39;Start&#39; : &#39;End&#39;) +
<a name="line506"></a>      &#39;To&#39; +
<a name="line507"></a>      (otherEndpoint == goog.dom.RangeEndpoint.START ? &#39;Start&#39; : &#39;End&#39;),
<a name="line508"></a>      range);
<a name="line509"></a>};
<a name="line510"></a>
<a name="line511"></a>
<a name="line512"></a>/**
<a name="line513"></a> * Recurses to find the correct node for the given endpoint.
<a name="line514"></a> * @param {goog.dom.RangeEndpoint} endpoint The endpoint to get the node for.
<a name="line515"></a> * @param {Node=} opt_node Optional node to start the search from.
<a name="line516"></a> * @return {Node} The deepest node containing the endpoint.
<a name="line517"></a> * @private
<a name="line518"></a> */
<a name="line519"></a>goog.dom.browserrange.IeRange.prototype.getEndpointNode_ = function(endpoint,
<a name="line520"></a>    opt_node) {
<a name="line521"></a>
<a name="line522"></a>  /** @type {Node} */
<a name="line523"></a>  var node = opt_node || this.getContainer();
<a name="line524"></a>
<a name="line525"></a>  // If we&#39;re at a leaf in the DOM, we&#39;re done.
<a name="line526"></a>  if (!node || !node.firstChild) {
<a name="line527"></a>    return node;
<a name="line528"></a>  }
<a name="line529"></a>
<a name="line530"></a>  var start = goog.dom.RangeEndpoint.START, end = goog.dom.RangeEndpoint.END;
<a name="line531"></a>  var isStartEndpoint = endpoint == start;
<a name="line532"></a>
<a name="line533"></a>  // Find the first/last child that overlaps the selection.
<a name="line534"></a>  // NOTE(user) : One of the children can be the magic &amp;nbsp; node. This
<a name="line535"></a>  // node will have only nodeType property as valid and accessible. All other
<a name="line536"></a>  // dom related properties like ownerDocument, parentNode, nextSibling etc
<a name="line537"></a>  // cause error when accessed. Therefore use the for-loop on childNodes to
<a name="line538"></a>  // iterate.
<a name="line539"></a>  for (var j = 0, length = node.childNodes.length; j &lt; length; j++) {
<a name="line540"></a>    var i = isStartEndpoint ? j : length - j - 1;
<a name="line541"></a>    var child = node.childNodes[i];
<a name="line542"></a>    var childRange;
<a name="line543"></a>    try {
<a name="line544"></a>      childRange = goog.dom.browserrange.createRangeFromNodeContents(child);
<a name="line545"></a>    } catch (e) {
<a name="line546"></a>      // If the child is the magic &amp;nbsp; node, then the above will throw
<a name="line547"></a>      // error. The magic node exists only when editing using keyboard, so can
<a name="line548"></a>      // not add any unit test.
<a name="line549"></a>      continue;
<a name="line550"></a>    }
<a name="line551"></a>    var ieRange = childRange.getBrowserRange();
<a name="line552"></a>
<a name="line553"></a>    // Case 1 : Finding end points when this range is collapsed.
<a name="line554"></a>    // Note that in case of collapsed range, getEnd{Node,Offset} call
<a name="line555"></a>    // getStart{Node,Offset}.
<a name="line556"></a>    if (this.isCollapsed()) {
<a name="line557"></a>      // Handle situations where caret is not in a text node. In such cases,
<a name="line558"></a>      // the adjacent child won&#39;t be a valid range endpoint container.
<a name="line559"></a>      if (!goog.dom.browserrange.canContainRangeEndpoint(child)) {
<a name="line560"></a>        // The following handles a scenario like &lt;div&gt;&lt;BR&gt;[caret]&lt;BR&gt;&lt;/div&gt;,
<a name="line561"></a>        // where point should be (div, 1).
<a name="line562"></a>        if (this.compareBrowserRangeEndpoints(ieRange, start, start) == 0) {
<a name="line563"></a>          this.startOffset_ = this.endOffset_ = i;
<a name="line564"></a>          return node;
<a name="line565"></a>        }
<a name="line566"></a>      } else if (childRange.containsRange(this)) {
<a name="line567"></a>        // For collapsed range, we should invert the containsRange check with
<a name="line568"></a>        // childRange.
<a name="line569"></a>        return this.getEndpointNode_(endpoint, child);
<a name="line570"></a>      }
<a name="line571"></a>
<a name="line572"></a>    // Case 2 - The first child encountered to have overlap this range is
<a name="line573"></a>    // contained entirely in this range.
<a name="line574"></a>    } else if (this.containsRange(childRange)) {
<a name="line575"></a>      // If it is an element which can not be a range endpoint container, the
<a name="line576"></a>      // current child offset can be used to deduce the endpoint offset.
<a name="line577"></a>      if (!goog.dom.browserrange.canContainRangeEndpoint(child)) {
<a name="line578"></a>
<a name="line579"></a>        // Container can&#39;t be any deeper, so current node is the container.
<a name="line580"></a>        if (isStartEndpoint) {
<a name="line581"></a>          this.startOffset_ = i;
<a name="line582"></a>        } else {
<a name="line583"></a>          this.endOffset_ = i + 1;
<a name="line584"></a>        }
<a name="line585"></a>        return node;
<a name="line586"></a>      }
<a name="line587"></a>
<a name="line588"></a>      // If child can contain range endpoints, recurse inside this child.
<a name="line589"></a>      return this.getEndpointNode_(endpoint, child);
<a name="line590"></a>
<a name="line591"></a>    // Case 3 - Partial non-adjacency overlap.
<a name="line592"></a>    } else if (this.compareBrowserRangeEndpoints(ieRange, start, end) &lt; 0 &amp;&amp;
<a name="line593"></a>               this.compareBrowserRangeEndpoints(ieRange, end, start) &gt; 0) {
<a name="line594"></a>      // If this child overlaps the selection partially, recurse down to find
<a name="line595"></a>      // the first/last child the next level down that overlaps the selection
<a name="line596"></a>      // completely. We do not consider edge-adjacency (== 0) as overlap.
<a name="line597"></a>      return this.getEndpointNode_(endpoint, child);
<a name="line598"></a>    }
<a name="line599"></a>
<a name="line600"></a>  }
<a name="line601"></a>
<a name="line602"></a>  // None of the children of this node overlapped the selection, that means
<a name="line603"></a>  // the selection starts/ends in this node directly.
<a name="line604"></a>  return node;
<a name="line605"></a>};
<a name="line606"></a>
<a name="line607"></a>
<a name="line608"></a>/**
<a name="line609"></a> * Compares one endpoint of this range with the endpoint of a node.
<a name="line610"></a> * For internal methods, we should prefer this method to containsNode.
<a name="line611"></a> * containsNode has a lot of false negatives when we&#39;re dealing with
<a name="line612"></a> * {@code &lt;br&gt;} tags.
<a name="line613"></a> *
<a name="line614"></a> * @param {Node} node The node to compare against.
<a name="line615"></a> * @param {goog.dom.RangeEndpoint} thisEndpoint The endpoint of this range
<a name="line616"></a> *     to compare with.
<a name="line617"></a> * @param {goog.dom.RangeEndpoint} otherEndpoint The endpoint of the node
<a name="line618"></a> *     to compare with.
<a name="line619"></a> * @return {number} 0 if the endpoints are equal, negative if this range
<a name="line620"></a> *     endpoint comes before the other node endpoint, and positive otherwise.
<a name="line621"></a> * @private
<a name="line622"></a> */
<a name="line623"></a>goog.dom.browserrange.IeRange.prototype.compareNodeEndpoints_ =
<a name="line624"></a>    function(node, thisEndpoint, otherEndpoint) {
<a name="line625"></a>  return this.range_.compareEndPoints(
<a name="line626"></a>      (thisEndpoint == goog.dom.RangeEndpoint.START ? &#39;Start&#39; : &#39;End&#39;) +
<a name="line627"></a>      &#39;To&#39; +
<a name="line628"></a>      (otherEndpoint == goog.dom.RangeEndpoint.START ? &#39;Start&#39; : &#39;End&#39;),
<a name="line629"></a>      goog.dom.browserrange.createRangeFromNodeContents(node).
<a name="line630"></a>          getBrowserRange());
<a name="line631"></a>};
<a name="line632"></a>
<a name="line633"></a>
<a name="line634"></a>/**
<a name="line635"></a> * Returns the offset into the start/end container.
<a name="line636"></a> * @param {goog.dom.RangeEndpoint} endpoint The endpoint to get the offset for.
<a name="line637"></a> * @param {Node=} opt_container The container to get the offset relative to.
<a name="line638"></a> *     Defaults to the value returned by getStartNode/getEndNode.
<a name="line639"></a> * @return {number} The offset.
<a name="line640"></a> * @private
<a name="line641"></a> */
<a name="line642"></a>goog.dom.browserrange.IeRange.prototype.getOffset_ = function(endpoint,
<a name="line643"></a>    opt_container) {
<a name="line644"></a>  var isStartEndpoint = endpoint == goog.dom.RangeEndpoint.START;
<a name="line645"></a>  var container = opt_container ||
<a name="line646"></a>      (isStartEndpoint ? this.getStartNode() : this.getEndNode());
<a name="line647"></a>
<a name="line648"></a>  if (container.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line649"></a>    // Find the first/last child that overlaps the selection
<a name="line650"></a>    var children = container.childNodes;
<a name="line651"></a>    var len = children.length;
<a name="line652"></a>    var edge = isStartEndpoint ? 0 : len - 1;
<a name="line653"></a>    var sign = isStartEndpoint ? 1 : - 1;
<a name="line654"></a>
<a name="line655"></a>    // We find the index in the child array of the endpoint of the selection.
<a name="line656"></a>    for (var i = edge; i &gt;= 0 &amp;&amp; i &lt; len; i += sign) {
<a name="line657"></a>      var child = children[i];
<a name="line658"></a>      // Ignore the child nodes, which could be end point containers.
<a name="line659"></a>      if (goog.dom.browserrange.canContainRangeEndpoint(child)) {
<a name="line660"></a>        continue;
<a name="line661"></a>      }
<a name="line662"></a>      // Stop looping when we reach the edge of the selection.
<a name="line663"></a>      var endPointCompare =
<a name="line664"></a>          this.compareNodeEndpoints_(child, endpoint, endpoint);
<a name="line665"></a>      if (endPointCompare == 0) {
<a name="line666"></a>        return isStartEndpoint ? i : i + 1;
<a name="line667"></a>      }
<a name="line668"></a>    }
<a name="line669"></a>
<a name="line670"></a>    // When starting from the end in an empty container, we erroneously return
<a name="line671"></a>    // -1: fix this to return 0.
<a name="line672"></a>    return i == -1 ? 0 : i;
<a name="line673"></a>  } else {
<a name="line674"></a>    // Get a temporary range object.
<a name="line675"></a>    var range = this.range_.duplicate();
<a name="line676"></a>
<a name="line677"></a>    // Create a range that selects the entire container.
<a name="line678"></a>    var nodeRange = goog.dom.browserrange.IeRange.getBrowserRangeForNode_(
<a name="line679"></a>        container);
<a name="line680"></a>
<a name="line681"></a>    // Now, intersect our range with the container range - this should give us
<a name="line682"></a>    // the part of our selection that is in the container.
<a name="line683"></a>    range.setEndPoint(isStartEndpoint ? &#39;EndToEnd&#39; : &#39;StartToStart&#39;, nodeRange);
<a name="line684"></a>
<a name="line685"></a>    var rangeLength = range.text.length;
<a name="line686"></a>    return isStartEndpoint ? container.length - rangeLength : rangeLength;
<a name="line687"></a>  }
<a name="line688"></a>};
<a name="line689"></a>
<a name="line690"></a>
<a name="line691"></a>/**
<a name="line692"></a> * Returns the text of the given node.  Uses IE specific properties.
<a name="line693"></a> * @param {Node} node The node to retrieve the text of.
<a name="line694"></a> * @return {string} The node&#39;s text.
<a name="line695"></a> * @private
<a name="line696"></a> */
<a name="line697"></a>goog.dom.browserrange.IeRange.getNodeText_ = function(node) {
<a name="line698"></a>  return node.nodeType == goog.dom.NodeType.TEXT ?
<a name="line699"></a>         node.nodeValue : node.innerText;
<a name="line700"></a>};
<a name="line701"></a>
<a name="line702"></a>
<a name="line703"></a>/**
<a name="line704"></a> * Tests whether this range is valid (i.e. whether its endpoints are still in
<a name="line705"></a> * the document).  A range becomes invalid when, after this object was created,
<a name="line706"></a> * either one or both of its endpoints are removed from the document.  Use of
<a name="line707"></a> * an invalid range can lead to runtime errors, particularly in IE.
<a name="line708"></a> * @return {boolean} Whether the range is valid.
<a name="line709"></a> */
<a name="line710"></a>goog.dom.browserrange.IeRange.prototype.isRangeInDocument = function() {
<a name="line711"></a>  var range = this.doc_.body.createTextRange();
<a name="line712"></a>  range.moveToElementText(this.doc_.body);
<a name="line713"></a>
<a name="line714"></a>  return this.containsRange(
<a name="line715"></a>      new goog.dom.browserrange.IeRange(range, this.doc_), true);
<a name="line716"></a>};
<a name="line717"></a>
<a name="line718"></a>
<a name="line719"></a>/** @override */
<a name="line720"></a>goog.dom.browserrange.IeRange.prototype.isCollapsed = function() {
<a name="line721"></a>  // Note(user) : The earlier implementation used (range.text == &#39;&#39;), but this
<a name="line722"></a>  // fails when (range.htmlText == &#39;&lt;br&gt;&#39;)
<a name="line723"></a>  // Alternative: this.range_.htmlText == &#39;&#39;;
<a name="line724"></a>  return this.range_.compareEndPoints(&#39;StartToEnd&#39;, this.range_) == 0;
<a name="line725"></a>};
<a name="line726"></a>
<a name="line727"></a>
<a name="line728"></a>/** @override */
<a name="line729"></a>goog.dom.browserrange.IeRange.prototype.getText = function() {
<a name="line730"></a>  return this.range_.text;
<a name="line731"></a>};
<a name="line732"></a>
<a name="line733"></a>
<a name="line734"></a>/** @override */
<a name="line735"></a>goog.dom.browserrange.IeRange.prototype.getValidHtml = function() {
<a name="line736"></a>  return this.range_.htmlText;
<a name="line737"></a>};
<a name="line738"></a>
<a name="line739"></a>
<a name="line740"></a>// SELECTION MODIFICATION
<a name="line741"></a>
<a name="line742"></a>
<a name="line743"></a>/** @override */
<a name="line744"></a>goog.dom.browserrange.IeRange.prototype.select = function(opt_reverse) {
<a name="line745"></a>  // IE doesn&#39;t support programmatic reversed selections.
<a name="line746"></a>  this.range_.select();
<a name="line747"></a>};
<a name="line748"></a>
<a name="line749"></a>
<a name="line750"></a>/** @override */
<a name="line751"></a>goog.dom.browserrange.IeRange.prototype.removeContents = function() {
<a name="line752"></a>  // NOTE: Sometimes htmlText is non-empty, but the range is actually empty.
<a name="line753"></a>  // TODO(gboyer): The htmlText check is probably unnecessary, but I left it in
<a name="line754"></a>  // for paranoia.
<a name="line755"></a>  if (!this.isCollapsed() &amp;&amp; this.range_.htmlText) {
<a name="line756"></a>    // Store some before-removal state.
<a name="line757"></a>    var startNode = this.getStartNode();
<a name="line758"></a>    var endNode = this.getEndNode();
<a name="line759"></a>    var oldText = this.range_.text;
<a name="line760"></a>
<a name="line761"></a>    // IE sometimes deletes nodes unrelated to the selection.  This trick fixes
<a name="line762"></a>    // that problem most of the time.  Even though it looks like a no-op, it is
<a name="line763"></a>    // somehow changing IE&#39;s internal state such that empty unrelated nodes are
<a name="line764"></a>    // no longer deleted.
<a name="line765"></a>    var clone = this.range_.duplicate();
<a name="line766"></a>    clone.moveStart(&#39;character&#39;, 1);
<a name="line767"></a>    clone.moveStart(&#39;character&#39;, -1);
<a name="line768"></a>
<a name="line769"></a>    // However, sometimes moving the start back and forth ends up changing the
<a name="line770"></a>    // range.
<a name="line771"></a>    // TODO(gboyer): This condition used to happen for empty ranges, but (1)
<a name="line772"></a>    // never worked, and (2) the isCollapsed call should protect against empty
<a name="line773"></a>    // ranges better than before.  However, this is left for paranoia.
<a name="line774"></a>    if (clone.text == oldText) {
<a name="line775"></a>      this.range_ = clone;
<a name="line776"></a>    }
<a name="line777"></a>
<a name="line778"></a>    // Use the browser&#39;s native deletion code.
<a name="line779"></a>    this.range_.text = &#39;&#39;;
<a name="line780"></a>    this.clearCachedValues_();
<a name="line781"></a>
<a name="line782"></a>    // Unfortunately, when deleting a portion of a single text node, IE creates
<a name="line783"></a>    // an extra text node unlike other browsers which just change the text in
<a name="line784"></a>    // the node.  We normalize for that behavior here, making IE behave like all
<a name="line785"></a>    // the other browsers.
<a name="line786"></a>    var newStartNode = this.getStartNode();
<a name="line787"></a>    var newStartOffset = this.getStartOffset();
<a name="line788"></a>    /** @preserveTry */
<a name="line789"></a>    try {
<a name="line790"></a>      var sibling = startNode.nextSibling;
<a name="line791"></a>      if (startNode == endNode &amp;&amp; startNode.parentNode &amp;&amp;
<a name="line792"></a>          startNode.nodeType == goog.dom.NodeType.TEXT &amp;&amp;
<a name="line793"></a>          sibling &amp;&amp; sibling.nodeType == goog.dom.NodeType.TEXT) {
<a name="line794"></a>        startNode.nodeValue += sibling.nodeValue;
<a name="line795"></a>        goog.dom.removeNode(sibling);
<a name="line796"></a>
<a name="line797"></a>        // Make sure to reselect the appropriate position.
<a name="line798"></a>        this.range_ = goog.dom.browserrange.IeRange.getBrowserRangeForNode_(
<a name="line799"></a>            newStartNode);
<a name="line800"></a>        this.range_.move(&#39;character&#39;, newStartOffset);
<a name="line801"></a>        this.clearCachedValues_();
<a name="line802"></a>      }
<a name="line803"></a>    } catch (e) {
<a name="line804"></a>      // IE throws errors on orphaned nodes.
<a name="line805"></a>    }
<a name="line806"></a>  }
<a name="line807"></a>};
<a name="line808"></a>
<a name="line809"></a>
<a name="line810"></a>/**
<a name="line811"></a> * @param {TextRange} range The range to get a dom helper for.
<a name="line812"></a> * @return {goog.dom.DomHelper} A dom helper for the document the range
<a name="line813"></a> *     resides in.
<a name="line814"></a> * @private
<a name="line815"></a> */
<a name="line816"></a>goog.dom.browserrange.IeRange.getDomHelper_ = function(range) {
<a name="line817"></a>  return goog.dom.getDomHelper(range.parentElement());
<a name="line818"></a>};
<a name="line819"></a>
<a name="line820"></a>
<a name="line821"></a>/**
<a name="line822"></a> * Pastes the given element into the given range, returning the resulting
<a name="line823"></a> * element.
<a name="line824"></a> * @param {TextRange} range The range to paste into.
<a name="line825"></a> * @param {Element} element The node to insert a copy of.
<a name="line826"></a> * @param {goog.dom.DomHelper=} opt_domHelper DOM helper object for the document
<a name="line827"></a> *     the range resides in.
<a name="line828"></a> * @return {Element} The resulting copy of element.
<a name="line829"></a> * @private
<a name="line830"></a> */
<a name="line831"></a>goog.dom.browserrange.IeRange.pasteElement_ = function(range, element,
<a name="line832"></a>    opt_domHelper) {
<a name="line833"></a>  opt_domHelper = opt_domHelper || goog.dom.browserrange.IeRange.getDomHelper_(
<a name="line834"></a>      range);
<a name="line835"></a>
<a name="line836"></a>  // Make sure the node has a unique id.
<a name="line837"></a>  var id;
<a name="line838"></a>  var originalId = id = element.id;
<a name="line839"></a>  if (!id) {
<a name="line840"></a>    id = element.id = goog.string.createUniqueString();
<a name="line841"></a>  }
<a name="line842"></a>
<a name="line843"></a>  // Insert (a clone of) the node.
<a name="line844"></a>  range.pasteHTML(element.outerHTML);
<a name="line845"></a>
<a name="line846"></a>  // Pasting the outerHTML of the modified element into the document creates
<a name="line847"></a>  // a clone of the element argument.  We want to return a reference to the
<a name="line848"></a>  // clone, not the original.  However we need to remove the temporary ID
<a name="line849"></a>  // first.
<a name="line850"></a>  element = opt_domHelper.getElement(id);
<a name="line851"></a>
<a name="line852"></a>  // If element is null here, we failed.
<a name="line853"></a>  if (element) {
<a name="line854"></a>    if (!originalId) {
<a name="line855"></a>      element.removeAttribute(&#39;id&#39;);
<a name="line856"></a>    }
<a name="line857"></a>  }
<a name="line858"></a>
<a name="line859"></a>  return element;
<a name="line860"></a>};
<a name="line861"></a>
<a name="line862"></a>
<a name="line863"></a>/** @override */
<a name="line864"></a>goog.dom.browserrange.IeRange.prototype.surroundContents = function(element) {
<a name="line865"></a>  // Make sure the element is detached from the document.
<a name="line866"></a>  goog.dom.removeNode(element);
<a name="line867"></a>
<a name="line868"></a>  // IE more or less guarantees that range.htmlText is well-formed &amp; valid.
<a name="line869"></a>  element.innerHTML = this.range_.htmlText;
<a name="line870"></a>  element = goog.dom.browserrange.IeRange.pasteElement_(this.range_, element);
<a name="line871"></a>
<a name="line872"></a>  // If element is null here, we failed.
<a name="line873"></a>  if (element) {
<a name="line874"></a>    this.range_.moveToElementText(element);
<a name="line875"></a>  }
<a name="line876"></a>
<a name="line877"></a>  this.clearCachedValues_();
<a name="line878"></a>
<a name="line879"></a>  return element;
<a name="line880"></a>};
<a name="line881"></a>
<a name="line882"></a>
<a name="line883"></a>/**
<a name="line884"></a> * Internal handler for inserting a node.
<a name="line885"></a> * @param {TextRange} clone A clone of this range&#39;s browser range object.
<a name="line886"></a> * @param {Node} node The node to insert.
<a name="line887"></a> * @param {boolean} before Whether to insert the node before or after the range.
<a name="line888"></a> * @param {goog.dom.DomHelper=} opt_domHelper The dom helper to use.
<a name="line889"></a> * @return {Node} The resulting copy of node.
<a name="line890"></a> * @private
<a name="line891"></a> */
<a name="line892"></a>goog.dom.browserrange.IeRange.insertNode_ = function(clone, node,
<a name="line893"></a>    before, opt_domHelper) {
<a name="line894"></a>  // Get a DOM helper.
<a name="line895"></a>  opt_domHelper = opt_domHelper || goog.dom.browserrange.IeRange.getDomHelper_(
<a name="line896"></a>      clone);
<a name="line897"></a>
<a name="line898"></a>  // If it&#39;s not an element, wrap it in one.
<a name="line899"></a>  var isNonElement;
<a name="line900"></a>  if (node.nodeType != goog.dom.NodeType.ELEMENT) {
<a name="line901"></a>    isNonElement = true;
<a name="line902"></a>    node = opt_domHelper.createDom(goog.dom.TagName.DIV, null, node);
<a name="line903"></a>  }
<a name="line904"></a>
<a name="line905"></a>  clone.collapse(before);
<a name="line906"></a>  node = goog.dom.browserrange.IeRange.pasteElement_(clone,
<a name="line907"></a>      /** @type {Element} */ (node), opt_domHelper);
<a name="line908"></a>
<a name="line909"></a>  // If we didn&#39;t want an element, unwrap the element and return the node.
<a name="line910"></a>  if (isNonElement) {
<a name="line911"></a>    // pasteElement_() may have returned a copy of the wrapper div, and the
<a name="line912"></a>    // node it wraps could also be a new copy. So we must extract that new
<a name="line913"></a>    // node from the new wrapper.
<a name="line914"></a>    var newNonElement = node.firstChild;
<a name="line915"></a>    opt_domHelper.flattenElement(node);
<a name="line916"></a>    node = newNonElement;
<a name="line917"></a>  }
<a name="line918"></a>
<a name="line919"></a>  return node;
<a name="line920"></a>};
<a name="line921"></a>
<a name="line922"></a>
<a name="line923"></a>/** @override */
<a name="line924"></a>goog.dom.browserrange.IeRange.prototype.insertNode = function(node, before) {
<a name="line925"></a>  var output = goog.dom.browserrange.IeRange.insertNode_(
<a name="line926"></a>      this.range_.duplicate(), node, before);
<a name="line927"></a>  this.clearCachedValues_();
<a name="line928"></a>  return output;
<a name="line929"></a>};
<a name="line930"></a>
<a name="line931"></a>
<a name="line932"></a>/** @override */
<a name="line933"></a>goog.dom.browserrange.IeRange.prototype.surroundWithNodes = function(
<a name="line934"></a>    startNode, endNode) {
<a name="line935"></a>  var clone1 = this.range_.duplicate();
<a name="line936"></a>  var clone2 = this.range_.duplicate();
<a name="line937"></a>  goog.dom.browserrange.IeRange.insertNode_(clone1, startNode, true);
<a name="line938"></a>  goog.dom.browserrange.IeRange.insertNode_(clone2, endNode, false);
<a name="line939"></a>
<a name="line940"></a>  this.clearCachedValues_();
<a name="line941"></a>};
<a name="line942"></a>
<a name="line943"></a>
<a name="line944"></a>/** @override */
<a name="line945"></a>goog.dom.browserrange.IeRange.prototype.collapse = function(toStart) {
<a name="line946"></a>  this.range_.collapse(toStart);
<a name="line947"></a>
<a name="line948"></a>  if (toStart) {
<a name="line949"></a>    this.endNode_ = this.startNode_;
<a name="line950"></a>    this.endOffset_ = this.startOffset_;
<a name="line951"></a>  } else {
<a name="line952"></a>    this.startNode_ = this.endNode_;
<a name="line953"></a>    this.startOffset_ = this.endOffset_;
<a name="line954"></a>  }
<a name="line955"></a>};
</pre>


</body>
</html>
